package com.lz.yz.outing.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.lz.yz.outing.config.OSSConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.util.HashMap;
import java.util.UUID;

@Service
public class UploadOSSFile {

    private static HashMap<String, String> imgTypes = new HashMap<>();

    static {
        imgTypes.put("pict", "image/pict");
        imgTypes.put("bmp", "image/bmp");
        imgTypes.put("gif", "image/gif");
        imgTypes.put("png", "image/png");
        imgTypes.put("jpe", "image/jpeg");
        imgTypes.put("jpeg", "image/jpeg");
        imgTypes.put("jpg", "image/jpeg");
        imgTypes.put("pct", "image/pict");
        imgTypes.put("pic", "image/pict");
        imgTypes.put("pict", "image/pict");
    }

    @Autowired
    OSSConfig ossConfig;

    /**
     * 上传文化
     *
     * @param file 文件对象
     * @throws IOException
     */
    public String uploadFile(File file) throws IOException {
        ObjectMetadata meta = new ObjectMetadata();
        String fileName = file.getName();
        String ffHouZhui = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
        String name = UUID.randomUUID().toString();
        meta.setContentType(imgTypes.get(ffHouZhui));
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
        // 上传文件
        InputStream inputStream = new FileInputStream(file);
        ossClient.putObject(ossConfig.getBucketName(), name + "." + ffHouZhui, inputStream, meta);
        // 关闭client
        ossClient.shutdown();
        return ossConfig.getFileUrl() + name + "." + ffHouZhui;
    }

    public String uploadFile(InputStream inputStream, String filePath) throws IOException {
        ObjectMetadata meta = new ObjectMetadata();
        String fileName = filePath.replaceAll(",","");
        String ffHouZhui = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
        String name = UUID.randomUUID().toString();
        meta.setContentType(imgTypes.get(ffHouZhui));
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
        ossClient.putObject(ossConfig.getBucketName(), name + "-" + fileName, inputStream, meta);
        // 关闭client
        ossClient.shutdown();
        return ossConfig.getFileUrl() + name + "-" + fileName;
    }

    public String uploadBase64File(String imageFile) throws IOException {

        // 通过base64来转化图片
        imageFile = imageFile.replaceAll("data:image/jpeg;base64,", "");
        BASE64Decoder decoder = new BASE64Decoder();
        // Base64解码
        byte[] imageByte = null;
        imageByte = decoder.decodeBuffer(imageFile);
        for (int i = 0; i < imageByte.length; ++i) {
            if (imageByte[i] < 0) {// 调整异常数据
                imageByte[i] += 256;
            }
        }
        // 生成文件名
        ByteArrayInputStream is = new ByteArrayInputStream(imageByte);
        // 生成文件
        String name = UUID.randomUUID().toString();
        // 创建OSSClient实例
        OSSClient ossClient = new OSSClient(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
        ossClient.putObject(ossConfig.getBucketName(), name + ".jpeg", is);
        // 关闭client
        ossClient.shutdown();
        return ossConfig.getFileUrl() + name + ".jpeg";
    }

    public static void inputstreamtofile(InputStream ins, File file) throws IOException {
        OutputStream os = new FileOutputStream(file);
        int bytesRead;
        byte[] buffer = new byte[8192];
        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
    }
}
